package findertestproject;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import ro.sergiu.finder.columns.ResultColumnFormatter;
import ro.sergiu.finder.filters.ComboFilter;
import ro.sergiu.finder.filters.FilterPanelUI;
import ro.sergiu.finder.filters.IntegerFilter;
import ro.sergiu.finder.filters.Operator;
import ro.sergiu.finder.filters.TextFilter;
import ro.sergiu.finder.models.columns.ColumnModel;
import ro.sergiu.finder.models.finder.FinderModel;
import ro.sergiu.finder.models.finder.db.DbFinderModel;
import ro.sergiu.finder.ui.RunSQL;
import ro.sergiu.finder.ui.SearchEngineSpecifications;

/**
 *
 * @author toporjinschi
 */
public class Customizer extends DbFinderModel<User> {

    private final Connection con;

    public Customizer(Connection con) {
        this.con = con;
    }

    @Override
    public String getTableName() {
        return "Person us";
    }

    @Override
    public List<ColumnModel> getColumns() {
        List<ColumnModel> columns = new ArrayList<ColumnModel>();
        columns.add(new ColumnModel("person_id", "ID").setDimension(20, 20, 20, 50));
        columns.add(new ColumnModel("firstname", "First name", new ResultColumnFormatter<String, String>() {
            @Override
            public String format(String value, ResultSet resultSet) throws SQLException {
                return value + " " + resultSet.getString("lastname");
            }
        }));
        columns.add(new ColumnModel("lastname", "Last name", false));
//        columns.add(new ColumnModel("pass", "Password"));
        return columns;
    }

    @Override
    public List<FilterPanelUI> getFilters() {
        List<FilterPanelUI> filters = new ArrayList<FilterPanelUI>();
        filters.add(new TextFilter<String>("First name", "firstname", Operator.LIKE));
        filters.add(new TextFilter<String>("Last name", "lastname", Operator.LIKE));
        filters.add(new IntegerFilter<String>("person_id", "person_id", Operator.LESS_OR_EQUALS));
        ComboFilter<String, Integer> comboFilter = new ComboFilter<String, Integer>("Test", "person_id", Operator.EQUALS);
        comboFilter.addItem("a", 1);
        comboFilter.addItem("b", 2);
//        ComboFilter<String, Integer> comboFilter = new ComboFilter<String, Integer>("Test", "id", Operator.EQUALS);
//        comboFilter.addItem("a", 1);
//        comboFilter.addItem("b", 2);
        filters.add(comboFilter);
        return filters;
    }

    @Override
    public User populateBean(ResultSet rs) throws SQLException {
        User user = new User();
        user.setId(rs.getInt("person_id"));
        user.setFirstName(rs.getString("firstname"));
        user.setLastName(rs.getString("lastname"));
        return user;
    }

    @Override
    public SearchEngineSpecifications getSearchEngine() {
        return new RunSQL<User>(con, true);
    }

    @Override
    public List<SearchTermColumn<String>> getSearchTermColumns() {
        List<SearchTermColumn<String>> searchTermColumns = new ArrayList<FinderModel.SearchTermColumn<String>>();
        searchTermColumns.add(new SearchTermColumn<String>("passWORD"));
//        searchTermColumns.add(new SearchTermColumn<String>("lastName"));
        return searchTermColumns;
    }

}